###############################################################
### Replication Code: Analysis of Pew's and Gallup's Data   ###
### Title: Can Conservatives Be Persuaded to Support UBI?   ###
### Author: Eddy S. F. Yeung                                ###
### Version: September 6, 2022                              ###
###############################################################

# Set-up ----
## Clean the R environment and set the working directory
rm(list = ls())
setwd("~/Desktop/UBI/PB_replication") # change to your own directory

## Load the required packages
library(tidyverse) # version 1.3.1
library(haven)     # version 2.4.3
library(extrafont) # version 0.17

## Import the datasets
pew_2018 <- read_sav("pew_Sept18_public.sav")
pew_2019 <- read_sav("pew_ATP_W53.sav")
pew_2020 <- read_sav("pew_ATP_W71.sav")
gallup_2019 <- read.csv("Gallup_2019_American-Dream.csv")

# Analysis ----
## First page in the main text: Americans' support for UBI by ideology ----
### Drop respondents who refused to report their ideology
pew_2020 <- subset(pew_2020, F_IDEO >= 1 & F_IDEO <= 5)

### Drop respondents who refused to report their UBI support
pew_2020 <- subset(pew_2020, UBI_W71 >= 1 & UBI_W71 <= 4)

### Define a dummy variable, with 1 = Very conservative/Conservative
pew_2020$conservative <- ifelse(pew_2020$F_IDEO == 1 | pew_2020$F_IDEO == 2, 1, 0)

### Define a dummy variable, with 1 = Very liberal/Liberal
pew_2020$liberal <- ifelse(pew_2020$F_IDEO == 4 | pew_2020$F_IDEO == 5, 1, 0)

### Define a dummy variable, with 1 = Strongly favor/Somewhat favor
pew_2020$support <- ifelse(pew_2020$UBI_W71 == 1 | pew_2020$UBI_W71 == 2, 1, 0)

### Analysis by group
pew_2020 %>% 
  group_by(conservative) %>% 
  summarize(mean = mean(support),
            sum = sum(support))
pew_2020 %>% 
  group_by(liberal) %>% 
  summarize(mean = mean(support),
            sum = sum(support))

## Subset the dataset for the "why poor" analysis (pew_2018)
df1 <- 
  pew_2018 %>% 
  drop_na(q64f1) %>% 
  filter(ideo != 9) %>% 
  filter(q64f1 != 9) %>% 
  rename(why_poor = q64f1)

## Reverse-code ideology (1 = Very Liberal; 5 = Very Conservative)
df1 <- 
  df1 %>% 
  mutate(ideology = 6 - ideo)

## Subset the dataset for the "why rich" analysis (pew_2018)
df2 <- 
  pew_2018 %>% 
  drop_na(q65f2) %>% 
  filter(ideo != 9) %>% 
  filter(q65f2 != 9) %>% 
  rename(why_rich = q65f2)

## Reverse-code ideology (1 = Very Liberal; 5 = Very Conservative)
df2 <- 
  df2 %>% 
  mutate(ideology = 6 - ideo)

## Figure A1: Ideological Divide on Why People Are Poor or Rich ----
### Analysis for "why poor"
matrix1 <- as.matrix(table(df1$why_poor, df1$ideology))
total1 <- c(sum(matrix1[ , 1]), sum(matrix1[ , 2]), sum(matrix1[ , 3]), 
            sum(matrix1[ , 4]), sum(matrix1[ , 5]))
temp <- rbind(matrix1, total1)
df_why_poor <-
  data.frame("Ideology" = c(1:5),
             "Percentage" = temp[1, ] / total1 * 100)
df_why_poor$Ideology <-
  factor(df_why_poor$Ideology,
         levels = c(1, 2, 3, 4, 5),
         labels = c("Very Liberal", "Liberal", "Moderate", "Conservative", "Very Conservative"))
plot <-
  ggplot(df_why_poor, aes(x = Ideology, y = Percentage, fill = Ideology)) +
  geom_bar(stat = "identity", width = 0.7, color = "black") +
  scale_fill_brewer(palette = "Blues") +
  xlab("Political Ideology") + 
  ylab("People Are Poor Generally Because\nof a Lack of Effort on Their Part (%)") + 
  theme_bw() +
  coord_cartesian(ylim = c(0, 100)) +
  theme(text = element_text(size = 12, family = "Times"),
        legend.position = "none",
        panel.grid = element_blank())
plot
ggsave("Pew_why-poor.pdf", width = 6, height = 4)

### Analysis for "why rich"
matrix2 <- as.matrix(table(df2$why_rich, df2$ideology))
total2 <- c(sum(matrix2[ , 1]), sum(matrix2[ , 2]), sum(matrix2[ , 3]), 
            sum(matrix2[ , 4]), sum(matrix2[ , 5]))
temp <- rbind(matrix2, total2)
df_why_rich <-
  data.frame("Ideology" = c(1:5),
             "Percentage" = temp[1, ] / total2 * 100)
df_why_rich$Ideology <-
  factor(df_why_rich$Ideology,
         levels = c(1, 2, 3, 4, 5),
         labels = c("Very Liberal", "Liberal", "Moderate", "Conservative", "Very Conservative"))
plot <-
  ggplot(df_why_rich, aes(x = Ideology, y = Percentage, fill = Ideology)) +
  geom_bar(stat = "identity", width = 0.7, color = "black") +
  scale_fill_brewer(palette = "Blues") +
  xlab("Political Ideology") + 
  ylab("People Are Rich Generally Because They\nWorked Harder Than Most Other People (%)") + 
  theme_bw() +
  coord_cartesian(ylim = c(0,100)) +
  theme(text = element_text(size = 12, family = "Times"),
        legend.position = "none",
        panel.grid = element_blank())
plot
ggsave("Pew_why-rich.pdf", width = 6, height = 4)
 
## Reorder the factors for the Gallup data (gallup_2019)
## Reorganized based on https://news.gallup.com/file/poll/260759/190717AmericanDream.pdf
gallup_2019$Ideology <- factor(gallup_2019$Ideology, 
                               levels = c("Liberal", "Moderate", "Conservative"))

## Figure A2: Ideological Divide on the Achievability of the American Dream ----
gallup_2019 <- gallup_2019 %>% mutate(Percentage = n_attainable / n_excluding_DK * 100)
plot <- 
  ggplot(gallup_2019, aes(x = Ideology, y = Percentage, fill = Ideology)) +
  geom_bar(stat = "identity", width = 0.5, color = "black") +
  scale_fill_brewer(palette = "Blues") +
  xlab("Political Ideology") + 
  ylab("American Dream Is Attainable by\nWorking Hard and Following Rules (%)") + 
  theme_bw() +
  coord_cartesian(ylim = c(0,100)) +
  theme(text = element_text(size = 14, family = "Times"),
        legend.position = "none",
        panel.grid = element_blank())
plot
ggsave("Gallup_american-dream.pdf", width = 5, height = 4)

## Subset the dataset for the government waste analysis (pew_2019)
df3 <- 
  pew_2019 %>% 
  drop_na(GOVWASTE_W53) %>% 
  filter(F_IDEO != 99) %>% 
  filter(GOVWASTE_W53 != 99) %>% 
  rename(ideo = F_IDEO, waste = GOVWASTE_W53)

## Reverse-code ideology (1 = Very Liberal; 5 = Very Conservative)
df3 <- 
  df3 %>% 
  mutate(ideology = 6 - ideo)

## Figure A3: Ideological Divide on Government Waste and Ineﬃciency ----
matrix <- as.matrix(table(df3$waste, df3$ideology))
total <- c(sum(matrix[ , 1]), sum(matrix[ , 2]), sum(matrix[ , 3]), 
           sum(matrix[ , 4]), sum(matrix[ , 5]))
temp <- rbind(matrix, total)
df_waste <-
  data.frame("Ideology" = c(1:5),
             "Percentage" = temp[1, ] / total * 100)
df_waste$Ideology <-
  factor(df_waste$Ideology,
         levels = c(1, 2, 3, 4, 5),
         labels = c("Very Liberal", "Liberal", "Moderate", "Conservative", "Very Conservative"))
plot <-
  ggplot(df_waste, aes(x = Ideology, y = Percentage, fill = Ideology)) +
  geom_bar(stat = "identity", width = 0.7, color = "black") +
  scale_fill_brewer(palette = "Blues") +
  xlab("Political Ideology") + 
  ylab("Government Is Almost Always\nWasteful and Inefficient (%)") + 
  theme_bw() +
  coord_cartesian(ylim = c(0,100)) +
  theme(text = element_text(size = 12, family = "Times"),
        legend.position = "none",
        panel.grid = element_blank())
plot
ggsave("Pew_government-waste.pdf", width = 6, height = 4)